package tools;

import java.util.ArrayList;

import data.dictionary.Dictionary;
import data.vectors.DoubleVector;
import data.vectors.DoubleVectorIterator;

public class PriorFromPerceptronWeights {
	
	public static Dictionary dico;
	
	public static Double[][] getPrior(DoubleVector weight, int numW){
		
		DoubleVectorIterator iter = weight.getNonNullIterator();
		Double[][] r = new Double[2][numW];
		MathematicTool.randInit(r);
		MathematicTool.normalize(r);
		while(iter.hasNext()){
			iter.next();

			//System.out.println(iter.getCurrentFeature());
			int id = iter.getCurrentFeature();
			double v = iter.getCurrentValue();
			if(v>=0){
				r[0][id] += v;
			}
			else{
				r[1][id] += -v;
			}
		}
		MathematicTool.normalize(r);
		return r;
	}
	
	public static Double[][] getPriorTop10(DoubleVector weight, int numW){
		
		int size = 10;
		DoubleVectorIterator iter = weight.getNonNullIterator();
		
		// retrouver les TOP 10
		int[] indexPOS = zerosInt(size);
		int[] indexNEG = zerosInt(size);
		double minValuePOS = 0;
		//int minIndexPOS = -1;
		
		double minValueNEG = 0;
		//int minIndexNEG = -1;
		
		int cptPOS=0;
		int cptNEG=0;
		while(iter.hasNext()){
			iter.next();
			int id = iter.getCurrentFeature();
			double v = iter.getCurrentValue();
			
			if(v>0 && cptPOS<size){
				indexPOS[cptPOS] = id;
				cptPOS++;
				continue;
			}
			if(v<0 && cptNEG<size){
				indexNEG[cptNEG] = id;
				cptNEG++;
				continue;
			}
			
			if(v>minValuePOS){
				int minIndexPOS = findIndexMin(indexPOS, weight);
				indexPOS[minIndexPOS] = id;
				minValuePOS = findMin(indexPOS, weight);
			}
			if(v<-minValueNEG){
				int minIndexNEG = findIndexMax(indexNEG, weight);
				indexNEG[minIndexNEG] = id;
				minValueNEG = -findMax(indexNEG, weight);
			}
				
		}
		
		System.out.println("index POS:");
		for(int i:indexPOS){
			System.out.print(i+" "); System.out.print(dico.getObject(i)+" | ");
		}
		System.out.println();	
		System.out.println("index NEG:");
		for(int i:indexNEG){
			System.out.print(i+" ");System.out.print(dico.getObject(i)+" | ");
		}
		System.out.println();	
		
		Double[][] r = new Double[2][numW];
		MathematicTool.randInit(r);
		MathematicTool.normalize(r);
		
		for(int i:indexPOS)
			r[0][i] += 2;
		for(int i:indexNEG)
			r[1][i] += 2;
		
		
		MathematicTool.normalize(r);
		return r;
	}

	private static int[] zerosInt(int n) {
		int[] tab = new int[n];
		for(int i=0; i<n; i++)
			tab[i] = 0;
		return tab;
	}

	private static double findMax(int[] index, DoubleVector weight) {
		double v = weight.getValue(index[0]);
		for(int i:index)
			if(v<weight.getValue(i))
				v = weight.getValue(i);
		
		return v;
	}

	private static int findIndexMax(int[] index, DoubleVector weight) {
		double v = weight.getValue(index[0]);
		int indRet = 0;
		int cpt=0;
		for(int i:index){
			if(v<weight.getValue(i)){
				indRet = cpt;
				v = weight.getValue(i);
			}
			cpt++;
		}
		
		return indRet;
	}

	private static double findMin(int[] index, DoubleVector weight) {
		double v = weight.getValue(index[0]);
		for(int i:index)
			if(v>weight.getValue(i))
				v = weight.getValue(i);
		
		return v;
	}

	private static int findIndexMin(int[] index, DoubleVector weight) {
		double v = weight.getValue(index[0]);
		int indRet = 0;
		int cpt=0;
		for(int i:index){
			if(v>weight.getValue(i)){
				indRet = cpt;
				v = weight.getValue(i);
			}
			cpt++;
		}
		
		return indRet;
	}
	
}
